package com.is.add.util;

import java.io.*;
import java.net.URI;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {

    private Properties props;
    private URI uri;

    public PropertiesUtil(String fileName){
        readProperties(fileName);
    }
    private void readProperties(String fileName) {
        try {
            props = new Properties();
            InputStream fis =getClass().getResourceAsStream(fileName);
            props.load(fis);
            uri = this.getClass().getResource(fileName).toURI();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取某个属性
     */
    public String getProperty(String key){
        return props.getProperty(key);
    }

    /**
     * 获取所有属性，返回一个map,不常用
     * 可以试试props.putAll(t)
     */
    public Map<String, String> getAllProperty(){
        Map<String, String> map=new HashMap<>();
        Enumeration enu = props.propertyNames();
        while (enu.hasMoreElements()) {
            String key = enu.nextElement().toString();
            String value = props.getProperty(key);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 在控制台上打印出所有属性，调试时用。
     */
    public void printProperties(){
        props.list(System.out);
    }

    /**
     * 写入properties信息
     */
    public void writeProperties(String key, String value) {
        try {
            OutputStream fos = new FileOutputStream(new File(uri));
            props.setProperty(key, value);
            // 将此 Properties 表中的属性列表（键和元素对）写入输出流
            props.store(fos, "『comments』Update key：" + key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 增加属性文件值
     * @param key           key
     * @param value         value
     * @param file          文件路径
     */
    public static void addProperties(String key[], String value[], String file) {
        Properties iniFile = getProperties(file);
        FileOutputStream oFile = null;
        try {
            iniFile.put(key, value);
            oFile = new FileOutputStream(file, true);
            iniFile.store(oFile, "modify properties file");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oFile != null) {
                    oFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取配置文件
     * @return
     */
    public static Properties getProperties(String file) {
        Properties pro = null;
        // 从文件mdxbu.properties中读取网元ID和模块ID信息
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            pro = new Properties();
            pro.load(in);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return pro;
    }

    /**
     * 保存属性到文件中
     *
     * @param pro
     * @param file
     */
    public static void saveProperties(Properties pro, String file) {
        if (pro == null) {
            return;
        }
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(file, false);
            pro.store(oFile, "modify properties file");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oFile != null) {
                    oFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改属性文件
     *
     * @param key
     * @param value
     */
    public static void updateProperties(String key, String value, String file) {
        // key为空则返回
        if (key == null || "".equalsIgnoreCase(key)) {
            return;
        }
        Properties pro = getProperties(file);
        if (pro == null) {
            pro = new Properties();
        }
        pro.put(key, value);

        // 保存属性到文件中
        saveProperties(pro, file);
    }

    /*public static void main(String[] args) {
        updateProperties("key", "value", "D:/test.properties");
        System.out.println(getProperties("D:/test.properties").get("key"));

        PropertiesUtil util=new PropertiesUtil("src/dbConfig.properties");
        util.writeProperties("dbtype", "MSSQL");
    }*/
}
